<?php
require_once "BaseController.php";
require_once 'util/httputil.php';

class IndexController extends BaseController {
	
	//首页
	public function indexAction() {
		try {
			$qalistDAO = $this->orm->createDAO('_wenda_info');
			$qalistDAO->with_wenda_category(array('wd_cate_id'=>'t_cate_id'))->alias('cate');
			$qalistDAO ->selectLimit .= " and _wenda_info.wd_state != -2";
			
			$cateid = HttpUtil::getString("cid");   //分类信息
			if($cateid != ""){
				$cateDAO = $this->orm->createDAO("_wenda_category");
				$cateDAO ->findT_cate_id($cateid);
				$cateDAO = $cateDAO->get();
				$this->view->assign("cateinfo",$cateDAO);
				
				if($cateDAO[0]['t_cate_pid'] == 0){
					$catelistDAO = $this->orm->createDAO("_wenda_category");         //显示到二级分类
					$catelistDAO ->findT_cate_path('0,'.$cateDAO[0]['t_cate_id']);
					//$catelistDAO ->enableDebug();
					$catelistDAO = $catelistDAO->get();
				
					$count1 = count($catelistDAO);
					if(!empty($count1)){
						foreach($catelistDAO as $key => $val){
						    if($key+1 == $count1){
						        $arraystr .= $val['t_cate_id'];
						    }else {
						    	$arraystr .= $val['t_cate_id'].","; 
						    }
						}
						$qalistDAO ->selectLimit .= " and _wenda_info.wd_cate_id in(".$arraystr.")";
					}else {
						$qalistDAO ->selectLimit .= " and _wenda_info.wd_cate_id = ".$cateid;   //显示小分类
					}
				}else {
					$qalistDAO ->selectLimit .= " and _wenda_info.wd_cate_id = ".$cateid;   //显示小分类
				}
			}
			$orderinfo = HttpUtil::getString("order");//排序
			
			if($orderinfo == ""){
				$qalistDAO ->selectLimit .= " order by _wenda_info.wd_sticky desc";
			}elseif($orderinfo == "liulan") {
				$qalistDAO ->selectLimit .= " order by _wenda_info.wd_view_volume desc";
			}elseif($orderinfo == "linghuida") {
				$qalistDAO ->selectLimit .= " and _wenda_info.wd_answer_count = 0 order by _wenda_info.wd_sticky desc";
			}
			$this->view->assign("order",$orderinfo);
				
			$perPage = 12;   //每页显示12条记录
			//$qalistDAO ->enableDebug();
			$listinfo = $qalistDAO ->getPager(array('path'=>__PROJECTNAME__ . '/index/index','perPage'=>$perPage,'nextImg'=>'»','prevImg'=>'«'))->returnArray();
			foreach ($listinfo['pageData'] as $k => $v){
				$listinfo['pageData'][$k]['wd_datetime'] = $this->time_tran($v['wd_datetime']);
				if($listinfo['pageData'][$k]['wd_best_answer'] != ""){
					$listinfo['pageData'][$k]['wd_reply_array'] = $this->getbestreplyAction($v['wd_best_answer']);
				}
			}
			//print_r($listinfo);
			$this->view->assign($listinfo);
			
			$hot_users = $this->getHotUsers(3);
			foreach ($hot_users as $key => $row){
				$expert_users = $this->check_expert($row['user_id']);
				if($expert_users){
					$hot_users[$key]['expert'] = 1;
				}
				$hot_users[$key]['expertise'] = $this->getUserExpertise($row['user_id']);
			}
			$this->view->assign("hot_users",$hot_users);
			
			//公告
			$this->view->announcement = $this->orm->createDAO('_wenda_site_info')->findIs_post(1)->order('info_id desc')->get();
			
			echo $this->view->render('index/index.htm');
			exit;
		}catch(Exception $e) {
			$this->toErrorLogs($e);
			$this->_forward('index','error',null,array('status'=>500,'error_obj'=>$e));
		}
	}

	public function getbestreplyAction($id){
		try {
			$replyDAO = $this->orm->createDAO("_wenda_reply");
			$replyDAO ->findId($id);
			$replyDAO = $replyDAO ->get();
			return $replyDAO;
			
		}catch(Exception $e) {
			$this->toErrorLogs($e);
			$this->_forward('index','error',null,array('status'=>500,'error_obj'=>$e));
		}
	}
	
	public function getreplyAction($wid){
		try {
			$replyDAO = $this->orm->createDAO("_wenda_reply");
			$replyDAO ->findWd_reply_wid($wid);
			$replyDAO = $replyDAO ->get();
			return $replyDAO;
			
		}catch(Exception $e) {
			$this->toErrorLogs($e);
			$this->_forward('index','error',null,array('status'=>500,'error_obj'=>$e));
		}
	}
}